package com.example.mediademo;

import android.content.Context;
import android.media.MediaCodec;
import android.media.MediaFormat;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import com.example.mediademo.decode.extractor.AudioExtractor;
import com.example.mediademo.decode.extractor.VideoExtractor;
import com.example.mediademo.muxer.MyMuxer;

import java.nio.ByteBuffer;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Mp4Repack implements Runnable{

    public static final int START_MUXER = 9;
    private String filePath;
    /**
     * 音视频分离器
     */
    private AudioExtractor mAudioExtractor;
    private VideoExtractor mVideoExtractor;
    /**
     * 音视频封装器
     */
    private MyMuxer myMuxer;

    private MediaFormat audioFormat;
    private MediaFormat videoFormat;
    /**
     * 定义数据缓冲区
     */
    private ByteBuffer byteBuffer = ByteBuffer.allocate(500 * 1024);
    private MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();

    public Mp4Repack(Context context,String filePath) {
        this.filePath = filePath;
        mAudioExtractor = new AudioExtractor(filePath);
        mVideoExtractor = new VideoExtractor(filePath);
        myMuxer = new MyMuxer(context);
    }

    public void start() {
        // 通过分离器获取音视频格式
        audioFormat = mAudioExtractor.getFormat();
        videoFormat = mVideoExtractor.getFormat();
        // 判断是否有音频数据，如果没有音频数据，则忽略音频通道
        if (audioFormat != null) {
            myMuxer.addAudioTrack(audioFormat);
        } else {
            myMuxer.setNoAudio();
        }
        // 判断是否有视频数据
        if (videoFormat != null) {
            myMuxer.addVideoTrack(videoFormat);
        } else {
            myMuxer.setNoVideo();
        }
        // 启动线程
        ExecutorService executorService = new ThreadPoolExecutor(2,20,60, TimeUnit.SECONDS,new LinkedBlockingQueue<>());
        executorService.execute(this);
    }

    @Override
    public void run() {
        HandlerFactory.getMainHandler().sendEmptyMessage(START_MUXER);
        if (audioFormat != null) {
            int size = mAudioExtractor.readBuffer(byteBuffer);
            while (size > 0) {
                bufferInfo.set(0,size,mAudioExtractor.getCurrentTimeStamp(),
                        mAudioExtractor.getSampleFlag());
                myMuxer.writeAudioData(byteBuffer,bufferInfo);
                size = mAudioExtractor.readBuffer(byteBuffer);
            }
        }
        if (videoFormat != null) {
            int size = mVideoExtractor.readBuffer(byteBuffer);
            while (size > 0) {
                bufferInfo.set(0,size,mVideoExtractor.getCurrentTimeStamp(),
                        mVideoExtractor.getSampleFlag());
                myMuxer.writeVideoData(byteBuffer,bufferInfo);
                size = mVideoExtractor.readBuffer(byteBuffer);
            }
        }
        mAudioExtractor.stop();
        mVideoExtractor.stop();
        myMuxer.release();

        Log.d("Muxer","打包完成");
    }
}
